package com.ssd.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssd.entity.operation.dto.SysRoleDto;
import com.ssd.entity.operation.entity.SysMenu;
import com.ssd.entity.operation.entity.SysRole;
import com.ssd.entity.operation.entity.SysRoleUser;
import com.ssd.mapper.SysMenuMapper;
import com.ssd.mapper.SysRoleMapper;
import com.ssd.mapper.SysRoleUserMapper;
import com.ssd.service.SysMenuService;
import com.ssd.service.SysRoleService;
import com.ssd.service.SysRoleUserService;
import com.ssd.viewmodel.Pager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 */
@Service
@Slf4j
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {
    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysRoleUserMapper sysRoleUserMapper;
    @Resource
    private SysRoleUserService sysRoleUserService;
    //角色列表的方法
    @Override
    public PageInfo<SysRole> findByPage(SysRoleDto sysRoleDto, Integer current, Integer limit) {
        //设置分页参数
        PageHelper.startPage(current,limit);
        //根据条件查询所有数据
        List<SysRole> list = sysRoleMapper.findByPage(sysRoleDto);
        //封装pageInfo对象
        PageInfo<SysRole> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    //2 角色添加的方法
    @Override
    public void saveSysRole(SysRole sysRole) {
        save(sysRole);
    }

    //3 角色修改的方法
    @Override
    public void updateSysRole(SysRole sysRole) {
        updateById(sysRole);
    }

    //4 角色删除的方法
    @Override
    public void deleteById(Long roleId) {
        removeById(roleId);
    }

    //查询所有角色
    @Override
    public Map<String, Object> findAll(Long userId) {
        //1 查询所有角色
        List<SysRole> roleList =  lambdaQuery().list();

        //2 分配过的角色列表
        //根据userId查询用户分配过角色id列表
        List<Long> roleIds = sysRoleUserService.lambdaQuery()
                .eq(SysRoleUser::getUserId,userId)
                .select(SysRoleUser::getRoleId).list().stream().map(SysRoleUser::getRoleId).collect(Collectors.toList());



        Map<String, Object> map = new HashMap<>();
        map.put("allRolesList",roleList);
        map.put("sysUserRoles",roleIds);

        return map;
    }

}
